#include "stage.h"
using namespace std;


#define NUM_DIVS 0

Stage::Stage() {
		//rectangleZv(Vector3f(0,0,0), Vector3f(40,0,40), COR_CHAO, NUM_DIVS);

		rectangleYv(Vector3f(0,10,0), Vector3f(40,0,0), COR_ENVOLTORIA, NUM_DIVS);
		rectangleYv(Vector3f(0,10,40), Vector3f(40,0,40), COR_ENVOLTORIA, NUM_DIVS);
		rectangleYv(Vector3f(0,10,40), Vector3f(0,0,0), COR_ENVOLTORIA, NUM_DIVS);
		rectangleYv(Vector3f(40,10,40), Vector3f(40,0,0), COR_ENVOLTORIA, NUM_DIVS);

		rectangleYv(Vector3f(36,10,40), Vector3f(36,0,38), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(36,10,36), Vector3f(36,0,11), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(32,10,20), Vector3f(32,0,11), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(30,10,7), Vector3f(30,0,3), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(30,10,1), Vector3f(30,0,0), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(25,10,30), Vector3f(25,0,11), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(20,10,40), Vector3f(20,0,30), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(18,10,20), Vector3f(18,0,6), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(14,10,33), Vector3f(14,0,30), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(10,10,40), Vector3f(10,0,38), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(10,10,36), Vector3f(10,0,25), COR_HORIZONTAL, NUM_DIVS);
		rectangleYv(Vector3f(10,10,20), Vector3f(10,0,18), COR_HORIZONTAL, NUM_DIVS);

		rectangleYv(Vector3f(10,10,16), Vector3f(10,0,4), COR_HORIZONTAL, NUM_DIVS);
		
		rectangleYv(Vector3f(14,10,33), Vector3f(18,0,33), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(10,10,30), Vector3f(22,0,30), COR_VERTICAL, NUM_DIVS);

		rectangleYv(Vector3f(24,10,30), Vector3f(32,0,30), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(34,10,30), Vector3f(36,0,30), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(0,10,25), Vector3f(4,0,25), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(6,10,25), Vector3f(16,0,25), COR_VERTICAL, NUM_DIVS);

		rectangleYv(Vector3f(0,10,20), Vector3f(18,0,20), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(25,10,20), Vector3f(32,0,20), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(25,10,11), Vector3f(27,0,11), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(29,10,11), Vector3f(33,0,11), COR_VERTICAL, NUM_DIVS);

		rectangleYv(Vector3f(35,10,11), Vector3f(36,0,11), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(18,10,7), Vector3f(36,0,7), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(38,10,7), Vector3f(40,0,7), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(10,10,6), Vector3f(13,0,6), COR_VERTICAL, NUM_DIVS);

		rectangleYv(Vector3f(15,10,6), Vector3f(18,0,6), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(18,10,4), Vector3f(30,0,4), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(0,10,4), Vector3f(3,0,4), COR_VERTICAL, NUM_DIVS);
		rectangleYv(Vector3f(5,10,4), Vector3f(10,0,4), COR_VERTICAL, NUM_DIVS);
}

#include <cstdio>
void Stage::draw() {
	printf("here %d %d\n", colors.size(), points.size());

	glColor3f(0.8,0.8,0.8);
	rectangleZ(Vector3f(0,0,0), Vector3f(40,0,40), COR_CHAO, NUM_DIVS);

	glColor3f(1,0,0);
	glEnableClientState(GL_VERTEX_ARRAY);
	//glEnableClientState(GL_COLOR_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, &points[0]);
	glColorPointer(4, GL_FLOAT, 0, &colors[0]);
	glDrawArrays(GL_QUADS, 0, points.size()/3);
	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
}

/*
void Stage::draw() {
	
	glEnable(GL_LINE_SMOOTH);
	//glBegin(GL_QUADS);
		rectangleZ(Vector3f(0,0,0), Vector3f(40,0,40), COR_CHAO, NUM_DIVS);
		//rectangleZ(Vector3f(0,10,0), Vector3f(40,10,40), COR_CHAO, NUM_DIVS);
		
		rectangleY(Vector3f(0,10,0), Vector3f(40,0,0), COR_ENVOLTORIA, NUM_DIVS);
		rectangleY(Vector3f(0,10,40), Vector3f(40,0,40), COR_ENVOLTORIA, NUM_DIVS);
		rectangleY(Vector3f(0,10,40), Vector3f(0,0,0), COR_ENVOLTORIA, NUM_DIVS);
		rectangleY(Vector3f(40,10,40), Vector3f(40,0,0), COR_ENVOLTORIA, NUM_DIVS);

		rectangleY(Vector3f(36,10,40), Vector3f(36,0,38), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(36,10,36), Vector3f(36,0,11), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(32,10,20), Vector3f(32,0,11), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(30,10,7), Vector3f(30,0,3), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(30,10,1), Vector3f(30,0,0), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(25,10,30), Vector3f(25,0,11), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(20,10,40), Vector3f(20,0,30), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(18,10,20), Vector3f(18,0,6), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(14,10,33), Vector3f(14,0,30), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(10,10,40), Vector3f(10,0,38), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(10,10,36), Vector3f(10,0,25), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(10,10,20), Vector3f(10,0,18), COR_HORIZONTAL, NUM_DIVS);
		rectangleY(Vector3f(10,10,16), Vector3f(10,0,4), COR_HORIZONTAL, NUM_DIVS);
		
		rectangleY(Vector3f(14,10,33), Vector3f(18,0,33), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(10,10,30), Vector3f(22,0,30), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(24,10,30), Vector3f(32,0,30), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(34,10,30), Vector3f(36,0,30), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(0,10,25), Vector3f(4,0,25), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(6,10,25), Vector3f(16,0,25), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(0,10,20), Vector3f(18,0,20), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(25,10,20), Vector3f(32,0,20), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(25,10,11), Vector3f(27,0,11), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(29,10,11), Vector3f(33,0,11), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(35,10,11), Vector3f(36,0,11), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(18,10,7), Vector3f(36,0,7), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(38,10,7), Vector3f(40,0,7), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(10,10,6), Vector3f(13,0,6), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(15,10,6), Vector3f(18,0,6), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(18,10,4), Vector3f(30,0,4), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(0,10,4), Vector3f(3,0,4), COR_VERTICAL, NUM_DIVS);
		rectangleY(Vector3f(5,10,4), Vector3f(10,0,4), COR_VERTICAL, NUM_DIVS);


	//glEnd();
}
*/

void Stage::rectangleZ(Vector3f supEsq, Vector3f infDir, Vector3f color, int sub, bool gambi) {
	if(gambi) {
		glLineWidth(7);
		glBegin(GL_LINE_LOOP);
			glColor3f(0,0,0);
			glVertex3f(supEsq.getX(), supEsq.getY(), supEsq.getZ());
			glVertex3f(supEsq.getX(), supEsq.getY(), infDir.getZ());
			glVertex3f(infDir.getX(), infDir.getY(), infDir.getZ());
			glVertex3f(infDir.getX(), infDir.getY(), supEsq.getZ());
			glVertex3f(supEsq.getX(), supEsq.getY(), supEsq.getZ());
		glEnd();
	}
	
	if(sub == 0) {
		glBegin(GL_QUADS);	
			glColor3f(color.getX(), color.getY(), color.getZ());
			glVertex3f(supEsq.getX(), supEsq.getY(), supEsq.getZ());
			glVertex3f(supEsq.getX(), supEsq.getY(), infDir.getZ());
			glVertex3f(infDir.getX(), infDir.getY(), infDir.getZ());
			glVertex3f(infDir.getX(), infDir.getY(), supEsq.getZ());
		glEnd();
	}
	else {
		rectangleZ(Vector3f(supEsq.getX(), supEsq.getY(), supEsq.getZ()), Vector3f((supEsq.getX()+infDir.getX())/2, infDir.getY(), (supEsq.getZ()+infDir.getZ())/2), color, sub-1, false);
		rectangleZ(Vector3f((supEsq.getX()+infDir.getX())/2, supEsq.getY(), supEsq.getZ()), Vector3f(infDir.getX(), infDir.getY(), (supEsq.getZ()+infDir.getZ())/2), color, sub-1, false);
		rectangleZ(Vector3f(supEsq.getX(), supEsq.getY(), (supEsq.getZ()+infDir.getZ())/2), Vector3f((supEsq.getX()+infDir.getX())/2, infDir.getY(), infDir.getZ()), color, sub-1, false);
		rectangleZ(Vector3f((supEsq.getX()+infDir.getX())/2, supEsq.getY(), (supEsq.getZ()+infDir.getZ())/2), Vector3f(infDir.getX(), infDir.getY(), infDir.getZ()), color, sub-1, false);
	}
}

void Stage::rectangleZv(Vector3f supEsq, Vector3f infDir, Vector3f color, int sub, bool gambi) {
	glColor3f(color.getX(), color.getY(), color.getZ());
	points.push_back(supEsq.getX());
	points.push_back(supEsq.getY());
	points.push_back(supEsq.getZ());

	points.push_back(supEsq.getX());
	points.push_back(supEsq.getY());
	points.push_back(infDir.getZ());

	points.push_back(infDir.getX());
	points.push_back(infDir.getY());
	points.push_back(infDir.getZ());

	points.push_back(infDir.getX());
	points.push_back(infDir.getY());
	points.push_back(supEsq.getZ());
}

void Stage::rectangleY(Vector3f supEsq, Vector3f infDir, Vector3f color, int sub, bool gambi) {
		if(supEsq.getZ() == infDir.getZ()) {
			glVertex3f(supEsq.getX(), supEsq.getY(), supEsq.getZ()-0.5);
			glVertex3f(supEsq.getX(), infDir.getY(), supEsq.getZ()-0.5);
			glVertex3f(infDir.getX(), infDir.getY(), infDir.getZ()-0.5);
			glVertex3f(infDir.getX(), supEsq.getY(), infDir.getZ()-0.5);
			glVertex3f(supEsq.getX(), supEsq.getY(), supEsq.getZ()+0.5);
			glVertex3f(supEsq.getX(), infDir.getY(), supEsq.getZ()+0.5);
			glVertex3f(infDir.getX(), infDir.getY(), infDir.getZ()+0.5);
			glVertex3f(infDir.getX(), supEsq.getY(), infDir.getZ()+0.5);
		} else if(supEsq.getX() == infDir.getX()) {
			glVertex3f(supEsq.getX()-0.5, supEsq.getY(), supEsq.getZ());
			glVertex3f(supEsq.getX()-0.5, infDir.getY(), supEsq.getZ());
			glVertex3f(infDir.getX()-0.5, infDir.getY(), infDir.getZ());
			glVertex3f(infDir.getX()-0.5, supEsq.getY(), infDir.getZ());
			glVertex3f(supEsq.getX()+0.5, supEsq.getY(), supEsq.getZ());
			glVertex3f(supEsq.getX()+0.5, infDir.getY(), supEsq.getZ());
			glVertex3f(infDir.getX()+0.5, infDir.getY(), infDir.getZ());
			glVertex3f(infDir.getX()+0.5, supEsq.getY(), infDir.getZ());
		}

}

void Stage::rectangleYv(Vector3f supEsq, Vector3f infDir, Vector3f color, int sub, bool gambi) {
		if(supEsq.getZ() == infDir.getZ()) {
			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX());
			points.push_back(supEsq.getY());
			points.push_back(supEsq.getZ()-0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);	
			points.push_back(supEsq.getX());
			points.push_back(infDir.getY());
			points.push_back(supEsq.getZ()-0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX());
			points.push_back(infDir.getY());
			points.push_back(infDir.getZ()-0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX());
			points.push_back(supEsq.getY());
			points.push_back(infDir.getZ()-0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX());
			points.push_back(supEsq.getY());
			points.push_back(supEsq.getZ()+0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX());
			points.push_back(infDir.getY());
			points.push_back(supEsq.getZ()+0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX());
			points.push_back(infDir.getY());
			points.push_back(infDir.getZ()+0.5);

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX());
			points.push_back(supEsq.getY());
			points.push_back(infDir.getZ()+0.5);
		} else if(supEsq.getX() == infDir.getX()) {
			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX()-0.5);
			points.push_back(supEsq.getY());
			points.push_back(supEsq.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX()-0.5);
			points.push_back(infDir.getY());
			points.push_back(supEsq.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX()-0.5);
			points.push_back(infDir.getY());
			points.push_back(infDir.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX()-0.5);
			points.push_back(supEsq.getY());
			points.push_back(infDir.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX()+0.5);
			points.push_back(supEsq.getY());
			points.push_back(supEsq.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(supEsq.getX()+0.5);
			points.push_back(infDir.getY());
			points.push_back(supEsq.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX()+0.5);
			points.push_back(infDir.getY());
			points.push_back(infDir.getZ());

			colors.push_back(color.getX());
			colors.push_back(color.getY());
			colors.push_back(color.getZ());
			colors.push_back(1);
			points.push_back(infDir.getX()+0.5);
			points.push_back(supEsq.getY());
			points.push_back(infDir.getZ());
	}
	printf("aaa %d %d\n", colors.size(), points.size());
}
